<template>
  <!--  卡片区域  -->
  <n-card>
    <!--    工具栏    -->
    <n-space justify="end" style="padding-bottom: 15px;">
      <!--刷新-->
      <n-tooltip trigger="hover">
        <template #trigger>
          <n-button text style="font-size: 20px; width:22px; height:32px;" @click="refresh">
            <n-icon>
              <refresh-sharp />
            </n-icon>
          </n-button>
        </template>
        <span>刷新</span>
      </n-tooltip>
      <!--密度-->
      <n-popselect v-model:value="densityValue" :options="densityOptions" trigger="click">
        <n-tooltip trigger="hover">
          <template #trigger>
            <n-button text style="font-size: 20px; width:22px; height:32px; margin-left: 11px">
              <n-icon>
                <auto-fit-height20-regular />
              </n-icon>
            </n-button>
          </template>
          <span>密度</span>
        </n-tooltip>
      </n-popselect>
      <!--列设置-->
      <n-tooltip trigger="hover">
        <template #trigger>
          <n-popover trigger="click" placement="bottom-end">
            <template #trigger>
              <n-button text style="font-size: 20px; width:22px; height:32px; margin-bottom: 2px; margin-left: 11px">
                <n-icon>
                  <Settings20Regular />
                </n-icon>
              </n-button>
            </template>
            <template #header>
              <n-checkbox label="列展示" v-model:checked="allChecked" @update:checked="allChange" />
            </template>
            <n-space vertical>
              <n-checkbox v-for="item in checkboxList" :key="item.name" :label="item.name" v-model:checked="item.checked" @update:checked="itemChange" />
            </n-space>
          </n-popover>
        </template>
        <span>列设置</span>
      </n-tooltip>
    </n-space>
    <!--    数据表格    -->
    <n-data-table
        :columns="columns"
        :data="data"
        :pagination="pagination"
        :bordered="false"
        :size="densityValue"
        :loading="loading"
    />
  </n-card>
</template>

<script>
import {defineComponent, h, ref, reactive} from 'vue'
import {NButton, NForm, NFormItem, NInput, NTag, NFlex, NIcon} from "naive-ui";
import { Add } from "@vicons/ionicons5"
import { RefreshSharp } from "@vicons/material"
import { AutoFitHeight20Regular ,Settings20Regular} from "@vicons/fluent"
import { LeftOutlined , RightOutlined} from "@vicons/antd"
import { GetBrowseList } from "@/api/browse";

export default defineComponent({
  components: {
    Add,
    RefreshSharp,
    AutoFitHeight20Regular,
    Settings20Regular,
    LeftOutlined,
    RightOutlined
  },
  setup(){
    const loading = ref(false)
    /*  数据表格  */
    // 创建表格列模版
    const createColumns = () => {
      return [
        {
          title: "序号",
          key: "index",
          render(row, index) {
            return index + 1;
          }
        },
        {
          title: "用户名",
          key: "username"
        },
        {
          title: "名称",
          key: "name"
        },
        {
          title: "类型",
          key: "type",
          render(row) {
            switch (row.type) {
              case 0:
                return h(
                    NTag,
                    {
                      strong: true,
                      size: "small",
                      type: "success",
                    },
                    { default: () => "浏览分行" }
                )
              case 1:
                return h(
                    NTag,
                    {
                      strong: true,
                      size: "small",
                      type: "info",
                    },
                    { default: () => "浏览职位" }
                )
            }
          }
        },
        {
          title: "注册时间",
          key: "time",
        },
      ];
    };
    const columns = createColumns()
    // 表格数据
    let data = reactive([])
    // 总条数
    const total = ref(0);
    // 总页数
    const totalPage = ref(0);
    // 当前页
    const page = ref(0);
    // 当前获取数据最后一页
    // const lastPage = ref(1);
    // 分页设置
    const pagination = {
      prev:()=>{
        return h(
            NIcon,
            {
              onClick:()=>{
                if (page.value > 1) {
                  page.value --
                }
              }
            },
            {
              default: () => h(LeftOutlined)
            }
        )
      },
      next:()=>{
        return h(
            NIcon,
            {
              onClick:()=>{
                if (page.value < totalPage.value) {
                    page.value ++
                }
              }
            },
            {
              default: () => h(RightOutlined)
            }
        )
      },
      prefix:()=>{
        const range = [page.value * 10 - 9, page.value * 10];
        if (page.value === 0) {
          return `第 0 - 0 条 / 总共 ${total.value} 条`
        }else if (page.value === 1 && totalPage.value !== 1) {
          return `第 1 - ${range[1]} 条 / 总共 ${total.value} 条`;
        }else if (page.value === totalPage.value) {
          return `第 ${range[0]} - ${data.length} 条 / 总共 ${total.value} 条`;
        }else if (page.value > 1 && page.value < totalPage.value) {
          return `第 ${range[0]} - ${range[1]} 条 / 总共 ${total.value} 条`;
        }
      },
    }

    /*获取用户列表数据*/
    let getData = ()=>{
      // let per_page = ref(10)
      let per_page = ref(800)
      const param = {
        page:page.value,
        per_page:per_page.value
      }
      loading.value = true
      GetBrowseList(param).then(res => {
        console.log(res)
        const content = res.data.list
        console.log(content)
        let getDate = (time)=> {
          const dateTime = new Date(time)
          const year = dateTime.getFullYear()
          let month = (dateTime.getMonth() + 1)
          month =  month >= 10 ? month: `0${month}`
          let date = dateTime.getDate()
          date = date >= 10? date : `0${date}`
          let hour = dateTime.getHours()
          hour = hour >= 10? hour : `0${hour}`
          let minute = dateTime.getMinutes()
          minute = minute >= 10? minute : `0${minute}`
          let second = dateTime.getSeconds()
          second = second >= 10? second : `0${second}`
          return `${year}-${month}-${date} ${hour}:${minute}:${second}`
        }
        content.forEach((item, index) =>{
          data.push({
            key: index,
            username: item.user ? item.user.username : '-',
            name: item.type ? (item.job ? (item.job.position ? item.job.position.name : '-') : '-') : (item.company ? item.company.name : '-'),
            type: item.type,
            time: getDate(item.createdAt)
          })
        })
        total.value = res.meta.total
        // totalPage.value = Math.ceil(total.value / 10)
        totalPage.value = Math.ceil(data.length / 10)
        // lastPage.value = Math.ceil(data.length / 10)
        page.value = 1

        loading.value = false
      })
    }
    getData()


    /*  工具栏  */
    /*刷新*/
    function refresh() {
      loading.value = true
      setTimeout(() => {
        loading.value = false
      }, 1000)
    }

    /*密度*/
    // 密度事件数据
    const densityOptions = [
      {
        label: "默认",
        value: "large"
      },
      {
        label: "中等",
        value: "medium"
      },
      {
        label: "紧密",
        value: "small",
      },
    ]
    const densityValue = ref('medium')

    /*列设置*/
    // 复选框全选
    const allChecked = ref(true)
    // 复选框列表
    const checkboxList = ref([])
    // 列表添加数据
    for (let i = 0;i < columns.length; i++){
      checkboxList.value.push({name: columns[i].title, checked: true})
    }
    // 全选改变事件
    const allChange = ()=>{
      if (allChecked.value) {
        checkboxList.value.forEach(item => {item.checked = true})

      } else {
        checkboxList.value.forEach(item => {item.checked = false})

      }
    }
    // 复选框单个改变事件
    const itemChange = ()=>{
      if (checkboxList.value.every(item => item.checked)) {
        allChecked.value = true
      } else {
        allChecked.value = false
      }
    }


    return {
      densityOptions,
      densityValue,
      data,
      columns,
      pagination,
      loading,
      refresh,
      page,
      checkboxList,
      allChecked,
      allChange,
      itemChange

    }
  }

})
</script>

<style scoped>

</style>